package com.aier.service.foreign.impl;


import com.aier.bean.common.CommonCode;
import com.aier.bean.foreign.ForeignBase;
import com.aier.mapper.foreign.ForeignBaseMapper;
import com.aier.service.foreign.ForeignBaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 外贸基础数据 实现类
 */

@Service
public class ForeignBaseServiceImpl implements ForeignBaseService {

    @Autowired
    private ForeignBaseMapper foreignBaseMapper;

    @Override
    public void saveForeignBase(ForeignBase foreignBase) {
        foreignBaseMapper.insert(foreignBase);
    }

    @Override
    public List<ForeignBase> getForeignBaseByStaffId(String staffId) {
        QueryWrapper<ForeignBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ForeignBase::getStaffId,staffId);
        return foreignBaseMapper.selectList(queryWrapper);
    }

    @Override
    public void modifyForeignBase(ForeignBase foreignBase) {
        UpdateWrapper<ForeignBase> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(ForeignBase::getFbaCode,foreignBase.getFbaCode());
        foreignBaseMapper.update(foreignBase,updateWrapper);
    }

    @Override
    public void removeForeignBase(String fbaCode) {
        QueryWrapper<ForeignBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ForeignBase::getFbaCode, fbaCode); // 数据库字段名
        foreignBaseMapper.delete(queryWrapper);
    }

    @Override
    public ForeignBase getForeignBaseByFbaCode(String fbaCode) {
        QueryWrapper<ForeignBase> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ForeignBase::getFbaCode,fbaCode);
        return foreignBaseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ForeignBase> getAll() {
        return foreignBaseMapper.selectList(null);
    }

    @Override
    public void saveForwarderId(String fbaCode, String forwarderId,String selfTaxed) {
        UpdateWrapper<ForeignBase> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(ForeignBase::getFbaCode,fbaCode)
                .set(ForeignBase::getForwarderId,forwarderId)
                .set(ForeignBase::getSelfTaxed, selfTaxed);
        foreignBaseMapper.update(null,updateWrapper);
    }

    @Override
    public List<ForeignBase> getForeignBaseByStaffIdNotShipment(String staffId) {
        QueryWrapper<ForeignBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ForeignBase::getStaffId,staffId)
                .and(wrap ->
                        wrap.ne(ForeignBase::getIsShipment, CommonCode.HAS_SHIPPED)  // is_shipment <> ?
                                .or()
                                .isNull(ForeignBase::getIsShipment)  // is_shipment is null
                );
        return foreignBaseMapper.selectList(queryWrapper);
    }


}
